<?php

namespace App\Admin\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\MessageBag;
use App\crm_customer;
use App\crm_tourist;
use App\crm_duanxin;
use app\text\Ucpaas;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Encore\Admin\Facades\Admin;
class XiaoxiController extends Controller
{
    /**
     * 展示给定用户的信息。
     *
     * @param  int  $id
     * @return Response
     */
    public function zhuanyuan()
    {
        // 跟单提醒和回访提醒
        $list['gendan']=DB::table("crm_huifang")->where("user_id",Admin::user()->id)->where("date",date("Y-m-d"))->where("statue",1)->get();
        $a=DB::table("crm_huifang")->where("user_id",Admin::user()->id)->where("date",date("Y-m-d"))->where("statue",1)->count();
        $list['huifang']=DB::table("crm_dchuifang")->where("userid",Admin::user()->id)->where("date",date("Y-m-d"))->where("statue",1)->get();
        $e=DB::table("crm_dchuifang")->where("userid",Admin::user()->id)->where("date",date("Y-m-d"))->where("statue",1)->count();
        $list['shuliang']=$a+$e;
        // 出发提醒
        $list['chufa']=DB::table("crm_deal")->where("deal_userid",Admin::user()->id)->where("deal_ftdate",date("Y-m-d", strtotime(date("Y-m-d"))+3600*24))->where("deal_xiaoxi",1)->get();
        $b=DB::table("crm_deal")->where("deal_userid",Admin::user()->id)->where("deal_ftdate",date("Y-m-d", strtotime(date("Y-m-d"))+3600*24))->where("deal_xiaoxi",1)->count();
        $list['fatuan']=$b;
        // 请款审批，收款审批，核算审批消息提醒 
        $list['skxiaoxi']=DB::table("crm_receive")->where("receive_userid",Admin::user()->id)->where("receive_xiaoxi",2)->get();
        $c=DB::table("crm_receive")->where("receive_userid",Admin::user()->id)->where("receive_xiaoxi",2)->count();
        $list['qkxiaoxi']=DB::table("crm_funds")->where("funds_userid",Admin::user()->id)->where("funds_xiaoxi",2)->get();
        $d=DB::table("crm_funds")->where("funds_userid",Admin::user()->id)->where("funds_xiaoxi",2)->count();
        $list['jsxiaoxi']=DB::table("crm_deal")->where("deal_userid",Admin::user()->id)->where("deal_jsxiaoxi",2)->get();
        $f=DB::table("crm_deal")->where("deal_userid",Admin::user()->id)->where("deal_jsxiaoxi",2)->count();
        $list['qkshu']=$c+$d+$f;
        return json_encode($list);
    }
    public function caiwu()
    {
        // 跟单提醒和回访提醒
        $list['gendan']=DB::table("crm_huifang")->where("user_id",Admin::user()->id)->where("date",date("Y-m-d"))->where("statue",1)->get();
        $a=DB::table("crm_huifang")->where("user_id",Admin::user()->id)->where("date",date("Y-m-d"))->where("statue",1)->count();
        $list['huifang']=DB::table("crm_dchuifang")->where("userid",Admin::user()->id)->where("date",date("Y-m-d"))->where("statue",1)->get();
        $e=DB::table("crm_dchuifang")->where("userid",Admin::user()->id)->where("date",date("Y-m-d"))->where("statue",1)->count();
        // 收款审批提醒
        $list['skshenpi']=DB::table("crm_receive")->where("receive_statue",1)->get();
        $g=DB::table("crm_receive")->where("receive_statue",1)->count();
        $list['shuliang']=$a+$e+$g;

        // 出发提醒
        $list['chufa']=DB::table("crm_deal")->where("deal_userid",Admin::user()->id)->where("deal_ftdate",date("Y-m-d", strtotime(date("Y-m-d"))+3600*24))->where("deal_xiaoxi",1)->get();
        $b=DB::table("crm_deal")->where("deal_userid",Admin::user()->id)->where("deal_ftdate",date("Y-m-d", strtotime(date("Y-m-d"))+3600*24))->where("deal_xiaoxi",1)->count();
        // 请款审批提醒
        $list['qkshenpi']=DB::table("crm_funds")->where("funds_statue",1)->get();
        $h=DB::table("crm_funds")->where("funds_statue",1)->count();
        $list['fatuan']=$b+$h;

        // 请款审批，收款审批，核算审批消息提醒 
        $list['skxiaoxi']=DB::table("crm_receive")->where("receive_userid",Admin::user()->id)->where("receive_xiaoxi",2)->get();
        $c=DB::table("crm_receive")->where("receive_userid",Admin::user()->id)->where("receive_xiaoxi",2)->count();
        $list['qkxiaoxi']=DB::table("crm_funds")->where("funds_userid",Admin::user()->id)->where("funds_xiaoxi",2)->get();
        $d=DB::table("crm_funds")->where("funds_userid",Admin::user()->id)->where("funds_xiaoxi",2)->count();
        $list['jsxiaoxi']=DB::table("crm_deal")->where("deal_userid",Admin::user()->id)->where("deal_jsxiaoxi",2)->get();
        $f=DB::table("crm_deal")->where("deal_userid",Admin::user()->id)->where("deal_jsxiaoxi",2)->count();
        // 结算审批提醒 
        $list['jsshenpi']=DB::table("crm_deal")->where("deal_jiesuanstatue",2)->get();
        $i=DB::table("crm_deal")->where("deal_jiesuanstatue",2)->count();
        $list['qkshu']=$c+$d+$f+$i;
        return json_encode($list);
    }
    public function zongjingli()
    {
         // 跟单提醒和回访提醒
        $list['gendan']=DB::table("crm_huifang")->where("user_id",Admin::user()->id)->where("date",date("Y-m-d"))->where("statue",1)->get();
        $a=DB::table("crm_huifang")->where("user_id",Admin::user()->id)->where("date",date("Y-m-d"))->where("statue",1)->count();
        $list['huifang']=DB::table("crm_dchuifang")->where("userid",Admin::user()->id)->where("date",date("Y-m-d"))->where("statue",1)->get();
        $e=DB::table("crm_dchuifang")->where("userid",Admin::user()->id)->where("date",date("Y-m-d"))->where("statue",1)->count();
        $aa=DB::table("crm_deal")->where("deal_sshoukuan","<",1)->pluck("id");
        $list['dzshenpi']=DB::table("crm_funds")->whereIn('funds_dealid',$aa)->where("funds_statue",1)->get();
        $h=DB::table("crm_funds")->whereIn('funds_dealid',$aa)->where("funds_statue",1)->count();
        $list['shuliang']=$a+$e+$h;
        // 出发提醒
        $list['chufa']=DB::table("crm_deal")->where("deal_userid",Admin::user()->id)->where("deal_ftdate",date("Y-m-d", strtotime(date("Y-m-d"))+3600*24))->where("deal_xiaoxi",1)->get();
        $b=DB::table("crm_deal")->where("deal_userid",Admin::user()->id)->where("deal_ftdate",date("Y-m-d", strtotime(date("Y-m-d"))+3600*24))->where("deal_xiaoxi",1)->count();
        $list['fatuan']=$b;
        // 请款审批，收款审批，核算审批消息提醒 
        $list['skxiaoxi']=DB::table("crm_receive")->where("receive_userid",Admin::user()->id)->where("receive_xiaoxi",2)->get();
        $c=DB::table("crm_receive")->where("receive_userid",Admin::user()->id)->where("receive_xiaoxi",2)->count();
        $list['qkxiaoxi']=DB::table("crm_funds")->where("funds_userid",Admin::user()->id)->where("funds_xiaoxi",2)->get();
        $d=DB::table("crm_funds")->where("funds_userid",Admin::user()->id)->where("funds_xiaoxi",2)->count();
        $list['jsxiaoxi']=DB::table("crm_deal")->where("deal_userid",Admin::user()->id)->where("deal_jsxiaoxi",2)->get();
        $f=DB::table("crm_deal")->where("deal_userid",Admin::user()->id)->where("deal_jsxiaoxi",2)->count();
        $list['qkshu']=$c+$d+$f;
        return json_encode($list);
    }
    public function fatuan($deal,$order)
    {
        $deal=DB::table("crm_deal")->where("id",$deal)->update(['deal_xiaoxi' => 2]);
        if($deal){
            return redirect(admin_url('crm_order/'.$order.'/edit'));
        }
    }
    public function skxiaoxi($receive,$order)
    {
        $deal=DB::table("crm_receive")->where("id",$receive)->update(['receive_xiaoxi' => 1]);
        if($deal){
            return redirect(admin_url('crm_order/'.$order.'/edit'));
        }
    }
    public function qkxiaoxi($funds,$order)
    {
        $deal=DB::table("crm_funds")->where("id",$funds)->update(['funds_xiaoxi' => 1]);
        if($deal){
            return redirect(admin_url('crm_order/'.$order.'/edit'));
        }
    }
    public function jsxiaoxi($deal,$order)
    {
        $list=DB::table("crm_deal")->where("id",$deal)->update(['deal_jsxiaoxi' => 1]);
        if($deal){
            return redirect(admin_url('crm_order/'.$order.'/edit'));
        }
    }
    // 客户关怀
    public function shengri($id)
    {
        //初始化必填
        $options['accountsid']='f6d88b775fd3b9f831a6af4cdbba0232';
        $options['token']='8d8d0f0f1ee3dbf5c32b6e10789802d2';
        //初始化 $options必填
        $ucpass = new Ucpaas($options);
        //开发者账号信息查询默认为json或xml
        header("Content-Type:text/html;charset=utf-8");
        //短信验证码（模板短信）,默认以65个汉字（同65个英文）为一条（可容纳字数受您应用名称占用字符影响），超过长度短信平台将会自动分割为多条发送。分割后的多条短信将按照具体占用条数计费。
        $appId = "3382ad37d9d644e3bb75320669269a75";
        $to = "".crm_customer::find($id)->customer_phone."";
        $templateId = "173366";
        $param="".crm_customer::find($id)->customer_name."";
        $res=json_decode($ucpass->templateSMS($appId,$to,$templateId,$param));
        
        if($res->{'resp'}->{'respCode'}==000000){
            $success = new MessageBag([
                'title'   => '发送成功',
            ]);
            DB::table("crm_customer")->where('id',$id)->update(['customer_srxinxi'=>2]);
            return redirect(admin_url('crm_guanhuai/'))->with(compact('success')); 
        }elseif($res->{'resp'}->{'respCode'}==105147){
            $error = new MessageBag([
                    'title'   => '发送过于频繁！',
            ]);
            return back()->with(compact('error'));
        }else{
            $error = new MessageBag([
                    'title'   => '发送失败！错误代码：'.$res->{'resp'}->{'respCode'},
            ]);
            return back()->with(compact('error'));
        }
    }
    // 游客关怀
    public function youke($id)
    {
        //初始化必填
        $options['accountsid']='f6d88b775fd3b9f831a6af4cdbba0232';
        $options['token']='8d8d0f0f1ee3dbf5c32b6e10789802d2';
        //初始化 $options必填
        $ucpass = new Ucpaas($options);
        //开发者账号信息查询默认为json或xml
        header("Content-Type:text/html;charset=utf-8");
        //短信验证码（模板短信）,默认以65个汉字（同65个英文）为一条（可容纳字数受您应用名称占用字符影响），超过长度短信平台将会自动分割为多条发送。分割后的多条短信将按照具体占用条数计费。
        $appId = "3382ad37d9d644e3bb75320669269a75";
        $to = "".crm_tourist::find($id)->tourist_phone."";
        $templateId = "173366";
        $param="".crm_tourist::find($id)->tourist_name."";
        $res=json_decode($ucpass->templateSMS($appId,$to,$templateId,$param));
        if($res->{'resp'}->{'respCode'}==000000){
            $success = new MessageBag([
                'title'   => '发送成功',
            ]);
            DB::table("crm_tourist")->where('id',$id)->update(['tourist_xinxi'=>2]);
            return redirect(admin_url('crm_ykguanhuai/'))->with(compact('success')); 
        }elseif($res->{'resp'}->{'respCode'}==105147){
            $error = new MessageBag([
                    'title'   => '发送过于频繁！',
            ]);
            return back()->with(compact('error'));
        }else{
            $error = new MessageBag([
                    'title'   => '发送失败！错误代码：'.$res->{'resp'}->{'respCode'},
            ]);
            return back()->with(compact('error'));
        }
    }
    // 自定义短信发送
    public function duanxin(Request $request)
    {
        if($request['customerid']==null){
            $error = new MessageBag([
                    'title'   => '未选择客户',
            ]);
            return back()->with(compact('error'));
        }else{
            $customerid = explode(",", $request['customerid']);
            $aa=0;
            $bb=0;
            foreach($customerid as $value)
            {
               //初始化必填
                $options['accountsid']='f6d88b775fd3b9f831a6af4cdbba0232';
                $options['token']='8d8d0f0f1ee3dbf5c32b6e10789802d2';
                //初始化 $options必填
                $ucpass = new Ucpaas($options);
                //开发者账号信息查询默认为json或xml
                header("Content-Type:text/html;charset=utf-8");
                //短信验证码（模板短信）,默认以65个汉字（同65个英文）为一条（可容纳字数受您应用名称占用字符影响），超过长度短信平台将会自动分割为多条发送。分割后的多条短信将按照具体占用条数计费。
                $appId = "3382ad37d9d644e3bb75320669269a75";
                $list=$request->except("_token");
                $customer=DB::table("crm_customer")->where('id',$value)->first();
                $to="".$customer->customer_phone."";
                $templateId = "174531";
                if($list['duanxinid']=="自定义短信"){
                    $param="[".$customer->customer_name."]:".$list['duanxin']."";
                }else{
                    $param="[".$customer->customer_name."]:".crm_duanxin::find($list['duanxinid'])->content."";
                }
                $res=json_decode($ucpass->templateSMS($appId,$to,$templateId,$param));
                if($res->{'resp'}->{'respCode'}==000000){
                    $aa+=1;
                }else{
                    $bb+=1;
                } 
            }
            $success = new MessageBag([
                        'title'   => '发送成功'.$aa.'条'.'发送失败'.$bb.'条',
                    ]);
            return redirect(admin_url('crm_customer'))->with(compact('success'));
        }
        
        
    }
    // 游客自定义短信发送
    public function duanxinyk(Request $request)
    {
        if($request['customerid']==null){
            $error = new MessageBag([
                    'title'   => '未选择客户',
            ]);
            return back()->with(compact('error'));
        }else{
            $customerid = explode(",", $request['customerid']);
            $aa=0;
            $bb=0;
            foreach($customerid as $value){
                //初始化必填
                $options['accountsid']='f6d88b775fd3b9f831a6af4cdbba0232';
                $options['token']='8d8d0f0f1ee3dbf5c32b6e10789802d2';
                //初始化 $options必填
                $ucpass = new Ucpaas($options);
                //开发者账号信息查询默认为json或xml
                header("Content-Type:text/html;charset=utf-8");
                //短信验证码（模板短信）,默认以65个汉字（同65个英文）为一条（可容纳字数受您应用名称占用字符影响），超过长度短信平台将会自动分割为多条发送。分割后的多条短信将按照具体占用条数计费。
                $appId = "3382ad37d9d644e3bb75320669269a75";
                $customer=DB::table("crm_tourist")->where('id',$value)->first();
                $templateId = "174531";
                if($list['duanxinid']=="自定义短信"){
                    $param="[".$customer->tourist_name."]:".$list['duanxin']."";
                }else{
                    $param="[".$customer->tourist_name."]:".crm_duanxin::find($list['duanxinid'])->content."";
                }
                $res=json_decode($ucpass->templateSMS($appId,$to,$templateId,$param));
                if($res->{'resp'}->{'respCode'}==000000){
                    $aa+=1;
                }else{
                    $bb+=1;
                } 
            }
            $success = new MessageBag([
                'title'   => '发送成功'.$aa.'条'.'发送失败'.$bb.'条',
            ]);
            return redirect(admin_url('crm_tourist'))->with(compact('success'));
        }
    }
}
